/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gss;

import org.apache.commons.math.fraction.BigFraction;
import java.math.BigInteger;

import java.io.Writer;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;

import java.io.Reader;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;
import java.util.Scanner;

/**
 *
 * @author mdm32
 */
public class FracToDouble {
    private static final String FRAC_REGEX = "(\\d+)(\\s*/\\s*(\\d+))?";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        // Process CLI arguments for INPUT ONLY
        Scanner scanner = null;
        String input_file = null;
        String output_file = null;
        BufferedWriter out = null;

        try {
            switch (args.length) {
                case 2:
                    input_file = args[0];
                    scanner = new Scanner(new BufferedReader(new FileReader(input_file)));
                    output_file = args[1];
                    break;
                case 1:
                    input_file = args[0];
                    scanner = new Scanner(new BufferedReader(new FileReader(input_file)));
                    output_file = input_file + ".gplot";
                    break;
		default:
		    System.err.println("Arguments: input_file [output_file]");
		    System.exit(1);
		    break;
            }

            FileWriter fstream = new FileWriter(output_file);
            out = new BufferedWriter(fstream);
            // declare some temporary variables
	    String num = null, denom = null;
	    BigFraction frac = null;

            // read in all data from file
            while (scanner.hasNext()) {
                Scanner line_scanner = new Scanner(scanner.nextLine());
                java.util.regex.MatchResult matcher = null;

		while (null != line_scanner.findInLine(FRAC_REGEX)) {
		    matcher = line_scanner.match();
		    num = matcher.group(1);
		    denom = matcher.group(3);
		    if (denom == null || denom.equals("")) {
			denom = "1";
		    }
		    frac = new BigFraction(new BigInteger(num), new BigInteger(denom));
		    out.write(frac.doubleValue() + " ");
		}
		
		out.newLine();
            }
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        } finally {
            if (scanner != null) {
                scanner.close();
            }

            if (out != null) {
                out.close();
            }
        }
    }
}
